package mc.contents.web.servlet;
import mc.contents.dao.ShopInfoMapper;
import mc.contents.pojo.persistence.ShopInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by LuoHui on 14-11-26.
 */
@WebServlet(name = "dsrResponse",urlPatterns="/dsrResponse",asyncSupported=false)
public class DsrResponseServlet extends HttpServlet {
    private ShopInfoMapper shopInfoMapper;

    @Override
    public void init( ServletConfig config ) throws ServletException {
        super.init( config );
        ServletContext servletContext = config.getServletContext();
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( servletContext );
        shopInfoMapper = (ShopInfoMapper) context.getBean( "shopInfoMapper" );
    }

    @Override
    protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        try{
            String nick = req.getParameter( "nick" );
            if(StringUtils.isNotEmpty( nick )){
                ShopInfo shopInfo = shopInfoMapper.getShopInfoByNick( nick );
                getPrams(shopInfo,req);
                shopInfoMapper.updateDsr( shopInfo );
            }
        }catch ( Exception e ){
            e.printStackTrace();
        }
    }

    private void getPrams( ShopInfo shopInfo, HttpServletRequest req ) {
        String score = req.getParameter( "score" );
        if(StringUtils.isNotEmpty( score )){
            score = score.replaceAll( ",","\n" );
            String[] ss = score.split( "\n" );
            List<String> l = new ArrayList<>(  );
            for ( String s : ss ) {
                s = s.trim();
                if(StringUtils.isNotBlank( s )){
                    l.add( s );
                }
            }
            if(l.size()>0){
                //商品描述评分
                String itemScore = l.get( 1 ).replace( "分","" ).replace( "暂无人打分","0" );
                String itemScoreLevel = l.get( 2 ).replace( "暂无人打分","0" );
                String itemScoreTotal = l.get( 4 ).replace( "共","" ).replace( "人","" ).replace( "暂无人打分","0" );
                String itemScore5 = l.get( 5 ).replace( "暂无人打分","0" );
                String itemScore4 = l.get( 6 ).replace( "暂无人打分","0" );
                String itemScore3 = l.get( 7 ).replace( "暂无人打分","0" );
                String itemScore2 = l.get( 8 ).replace( "暂无人打分","0" );
                String itemScore1 = l.get( 9 ).replace( "暂无人打分","0" );
                //卖家的服务态度：
                String serviceScore = l.get( 11 ).replace( "分", "" ).replace( "暂无人打分","0" );
                String serviceScoreLevel = l.get( 12 ).replace( "暂无人打分","0" );
                String serviceScoreTotal = l.get( 14 ).replace( "共","" ).replace( "人", "" ).replace( "暂无人打分","0" );
                String serviceScore5 = l.get( 15 ).replace( "暂无人打分","0" );
                String serviceScore4 = l.get( 16 ).replace( "暂无人打分","0" );
                String serviceScore3 = l.get( 17 ).replace( "暂无人打分","0" );
                String serviceScore2 = l.get( 18 ).replace( "暂无人打分","0" );
                String serviceScore1 = l.get( 19 ).replace( "暂无人打分","0" );
                //卖家发货的速度：
                String deliveryScore = l.get( 21 ).replace( "分","" ).replace( "暂无人打分","0" );
                String deliveryScoreLevel = l.get( 22 ).replace( "暂无人打分","0" );
                String deliveryScoreTotal = l.get( 24 ).replace( "共","" ).replace( "人","" ).replace( "暂无人打分","0" );
                String deliveryScore5 = l.get( 25 ).replace( "暂无人打分","0" );
                String deliveryScore4 = l.get( 26 ).replace( "暂无人打分","0" );
                String deliveryScore3 = l.get( 27 ).replace( "暂无人打分","0" );
                String deliveryScore2 = l.get( 28 ).replace( "暂无人打分","0" );
                String deliveryScore1 = l.get( 29 ).replace( "暂无人打分","0" );

                if( StringUtils.isNotBlank( itemScore )){
                    shopInfo.setItemScore( itemScore );
                }
                if( StringUtils.isNotBlank( itemScoreLevel )){
                    shopInfo.setItemScoreLevel( itemScoreLevel );
                }
                if( StringUtils.isNotBlank( itemScoreTotal )){
                    shopInfo.setItemScoreTotal( itemScoreTotal );
                }
                if( StringUtils.isNotBlank( itemScore1 )){
                    shopInfo.setItemScore1( itemScore1 );
                }
                if( StringUtils.isNotBlank( itemScore2 )){
                    shopInfo.setItemScore2( itemScore2 );
                }
                if( StringUtils.isNotBlank( itemScore3 )){
                    shopInfo.setItemScore3( itemScore3 );
                }
                if( StringUtils.isNotBlank( itemScore4 )){
                    shopInfo.setItemScore4( itemScore4 );
                }
                if( StringUtils.isNotBlank( itemScore5 )){
                    shopInfo.setItemScore5( itemScore5 );
                }
                if( StringUtils.isNotBlank( serviceScore )){
                    shopInfo.setServiceScore( serviceScore );
                }
                if( StringUtils.isNotBlank( serviceScoreLevel )){
                    shopInfo.setServiceScoreLevel( serviceScoreLevel );
                }
                if( StringUtils.isNotBlank( serviceScoreTotal )){
                    shopInfo.setServiceScoreTotal( serviceScoreTotal );
                }
                if( StringUtils.isNotBlank( serviceScore1 )){
                    shopInfo.setServiceScore1( serviceScore1 );
                }
                if( StringUtils.isNotBlank( serviceScore2 )){
                    shopInfo.setServiceScore2( serviceScore2 );
                }
                if( StringUtils.isNotBlank( serviceScore3 )){
                    shopInfo.setServiceScore3( serviceScore3 );
                }
                if( StringUtils.isNotBlank( serviceScore4 )){
                    shopInfo.setServiceScore4( serviceScore4 );
                }
                if( StringUtils.isNotBlank( serviceScore5 )){
                    shopInfo.setServiceScore5( serviceScore5 );
                }
                if( StringUtils.isNotBlank( deliveryScore )){
                    shopInfo.setDeliveryScore( deliveryScore );
                }
                if( StringUtils.isNotBlank( deliveryScoreLevel )){
                    shopInfo.setDeliveryScoreLevel( deliveryScoreLevel );
                }
                if( StringUtils.isNotBlank( deliveryScoreTotal )){
                    shopInfo.setDeliveryScoreTotal( deliveryScoreTotal );
                }
                if( StringUtils.isNotBlank( deliveryScore1 )){
                    shopInfo.setDeliveryScore1( deliveryScore1 );
                }
                if( StringUtils.isNotBlank( deliveryScore2 )){
                    shopInfo.setDeliveryScore2( deliveryScore2 );
                }
                if( StringUtils.isNotBlank( deliveryScore3 )){
                    shopInfo.setDeliveryScore3( deliveryScore3 );
                }
                if( StringUtils.isNotBlank( deliveryScore4 )){
                    shopInfo.setDeliveryScore4( deliveryScore4 );
                }
                if( StringUtils.isNotBlank( deliveryScore5 )){
                    shopInfo.setDeliveryScore5( deliveryScore5 );
                }
            }
        }
    }
}
